package edu.ccut.saturn.studio.wfg.dialog.routerdialog;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.ccut.saturn.studio.Activator;
import edu.ccut.saturn.studio.Images;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;
import edu.ccut.saturn.studio.wfg.dialog.RevertCandidate;
import edu.ccut.saturn.studio.wfg.dialog.SelectionCandidate;
import edu.ccut.saturn.studio.wfg.wfgmodel.SaturnRouterNode;

public class SaturnRouterDialog extends TitleAreaDialog {
	
	private SaturnRouterNode saturnRouterNode;
	
	private Text idText, nameText, descriptionText;
	
	private Label idLabel, nameLabel, descriptionLabel, offsetModeLabel, polymeModeLabel;
	
	private Combo offsetModelCombo, polymeModeCombo;
	
	public SaturnRouterDialog(Shell parentShell , SaturnRouterNode saturnRouterNode) {
		super(parentShell);
		this.saturnRouterNode = saturnRouterNode;
	}
	
	protected Control createContents(Composite parent) {
		Control contents = super.createContents(parent);
		setMessage(SelectionCandidate.SETTING_ATTRIBUTE + saturnRouterNode.getName());
		return contents;
	}
	
	protected Control createDialogArea(Composite parent) {
		
		getShell().setText(SelectionCandidate.ATTRIBUTE_SETTING);
		Composite area = (Composite) super.createDialogArea(parent);
		setTitleImage(Activator.getImage(Images.SATURN_ICONS_MAXNAME));

		final Composite composite = new Composite(area, SWT.NONE);
		final GridData gd_composite = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_composite.heightHint = 158;
		gd_composite.widthHint = 293;
		composite.setLayoutData(gd_composite);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 2;
		composite.setLayout(gridLayout);
		
		offsetModeLabel = new Label(composite, SWT.NONE);
		final GridData gd_offsetModeLabel = new GridData();
		gd_offsetModeLabel.verticalIndent = 10;
		offsetModeLabel.setLayoutData(gd_offsetModeLabel);
		offsetModeLabel.setText(Messages.getString("SaturnRouterDialog.BranchPattern"));   //$NON-NLS-1$
		
		offsetModelCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_offsetModelCombo = new GridData(80, SWT.DEFAULT);
		gd_offsetModelCombo.verticalIndent = 10;
		offsetModelCombo.setLayoutData(gd_offsetModelCombo);
		offsetModelCombo.setItems(SelectionCandidate.offsetModelCandidate);
		
		polymeModeLabel = new Label(composite, SWT.NONE);
		final GridData gd_polymeModeLabel = new GridData();
		gd_polymeModeLabel.verticalIndent = 10;
		polymeModeLabel.setLayoutData(gd_polymeModeLabel);
		polymeModeLabel.setText(Messages.getString("SaturnRouterDialog.ClusteringPattern"));   //$NON-NLS-1$

		polymeModeCombo = new Combo(composite, SWT.READ_ONLY);
		final GridData gd_polymeModeCombo = new GridData(80, SWT.DEFAULT);
		gd_polymeModeCombo.verticalIndent = 5;
		polymeModeCombo.setLayoutData(gd_polymeModeCombo);
		polymeModeCombo.setItems(SelectionCandidate.polymeModeCandidate);

		idLabel = new Label(composite, SWT.NONE);
		final GridData gd_idLabel = new GridData();
		gd_idLabel.verticalIndent = 10;
		idLabel.setLayoutData(gd_idLabel);
		idLabel.setText(Messages.getString("SaturnRouterDialog.ID")); //$NON-NLS-1$
		
		idText = new Text(composite, SWT.BORDER | SWT.READ_ONLY);
		final GridData gd_idText = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_idText.verticalIndent = 5;
		idText.setLayoutData(gd_idText);
		
		nameLabel = new Label(composite, SWT.NONE);
		final GridData gd_labelName = new GridData();
		gd_labelName.verticalIndent = 10;
		nameLabel.setLayoutData(gd_labelName);
		nameLabel.setText(Messages.getString("SaturnRouterDialog.Name"));   //$NON-NLS-1$
		
		nameText = new Text(composite, SWT.BORDER);
		nameText.setTextLimit(50);
		nameText.setFocus();
		final GridData gd_textName = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_textName.verticalIndent = 5;
		nameText.setLayoutData(gd_textName);

		descriptionLabel = new Label(composite, SWT.NONE);
		final GridData gd_labelDec = new GridData(SWT.LEFT, SWT.TOP, false, false);
		gd_labelDec.verticalIndent = 10;
		descriptionLabel.setLayoutData(gd_labelDec);
		descriptionLabel.setText(Messages.getString("SaturnRouterDialog.Decription"));   //$NON-NLS-1$

		descriptionText = new Text(composite, SWT.V_SCROLL | SWT.BORDER);
		descriptionText.setTextLimit(200);
		final GridData gd_textDec = new GridData(SWT.FILL, SWT.FILL, true, true);
		gd_textDec.verticalIndent = 5;
		gd_textDec.heightHint = 70;
		descriptionText.setLayoutData(gd_textDec);
		
		// 送到界面上
		idText.setText(saturnRouterNode.getId().trim());
		nameText.setText(saturnRouterNode.getName().trim());
		descriptionText.setText(saturnRouterNode.getDescription().trim());
		offsetModelCombo.setText(RevertCandidate.revertOffsetModel(saturnRouterNode.getOffsetMode().trim()));
		polymeModeCombo.setText(RevertCandidate.revertPolymeMode(saturnRouterNode.getPolymeMode().trim()));
		
		/**
		 * 名称不能为空
		 * 
		 * */
		nameText.addModifyListener(new ModifyListener() {
			public void modifyText(final ModifyEvent e) {
				if("".equals(nameText.getText().trim())){
					setErrorMessage(Messages.getString("SaturnRouterDialog.NameCannotEmpty"));   //$NON-NLS-1$
					getButton(IDialogConstants.OK_ID).setEnabled(false);
				} else {
					setErrorMessage(null);
					getButton(IDialogConstants.OK_ID).setEnabled(true);
				}
			}
		});
		
		return area;
	}
	
	protected Point getInitialSize() {
		return new Point(400, 340);
	}
	
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setImage(Activator.getImage(Images.SATURN_ICONS_SMALLNAME));
	}
	
	protected void buttonPressed(int buttonId) {
		saturnRouterNode.setName(nameText.getText().trim());
		saturnRouterNode.setDescription(descriptionText.getText().trim());
		saturnRouterNode.setPolymeMode(RevertCandidate.revertFromFaceToVO(polymeModeCombo.getText().trim()));
		saturnRouterNode.setOffsetMode(RevertCandidate.revertFromFaceToVO(offsetModelCombo.getText().trim()));
		super.buttonPressed(buttonId);
	}

	protected int getShellStyle() {
		return SWT.CLOSE | SWT.RESIZE;
	}
	
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		super.createButtonsForButtonBar(parent);
		getButton(IDialogConstants.OK_ID).setEnabled(true);
	}
}